class_name WeaponC_Capacity
extends WeaponC

@export var is_show_reload_progress_bar:bool
@export var reload_progress_bar_offest:Vector2
@export var reload_progress_bar: WeaponReloadProgressBar
var cur_count:int = 0:
	get:return cur_count
	set(value):
		var is_changed = cur_count != value
		cur_count = value
		if is_changed:
			on_cur_count_changed.emit(cur_count)
var is_reloading:bool = false
var weapon_data:WeaponData
var reload_reduction_scale:float = 1
signal on_start_reload
signal on_end_reload
signal on_cur_count_changed(cur_count:int)

func init_component(weapon:Weapon):
	super.init_component(weapon)
	weapon_data = weapon.weapon_data
	is_reloading = false
	cur_count = 0
	cur_count = min(weapon_data.clip_count,weapon_data.total_count) if weapon_data.total_count > 0 else weapon_data.clip_count
	weapon_data.total_count -= cur_count if weapon_data.total_count > 0 else 0
	reload_progress_bar.visible = false
	reload_progress_bar.init_pb(weapon_data.reload_time,true,on_reload_start,on_reload_completed)
func has_capacity(count:int) -> bool:
	return cur_count >= count or cur_count < 0
func get_from_capacity(count:int) -> int:
	if cur_count == 0:
		start_reload()
		return 0
	var res = min(cur_count,count) if cur_count > 0 else 0
	cur_count -= res
	return res if cur_count > 0 else count
func start_reload():
	if not reload_progress_bar or weapon_data.total_count == 0 or is_reloading:
		return
	is_reloading = true
	reload_reduction_scale = weapon.owner_entity.get_entity_data().get_reload_reduction_scale()
	reload_progress_bar.init_pb(weapon_data.reload_time * reload_reduction_scale)
	reload_progress_bar.start()
func end_reload():
	if not reload_progress_bar or not is_reloading:
		return
	reload_progress_bar.end()
	is_reloading = false
func on_reload_start():
	on_start_reload.emit()
func on_reload_completed():
	var count = 0
	if weapon_data.reload_count < 0:
		count = min(weapon_data.clip_count - cur_count,weapon_data.total_count) if weapon_data.total_count > 0 else weapon_data.clip_count - cur_count
	else:
		count = min(weapon_data.reload_count,weapon_data.total_count) if weapon_data.total_count > 0 else weapon_data.reload_count
	weapon_data.total_count -= count if weapon_data.total_count > 0 else 0
	cur_count += count
	on_end_reload.emit()
	is_reloading = false
func equip():
	super.equip()
	if reload_progress_bar and is_show_reload_progress_bar:
		if weapon.owner_entity and weapon.owner_entity.get_component_by_name("SlotComponent"):
			var head:Marker2D = weapon.owner_entity.get_component_by_name("SlotComponent").get_slot_by_type(GameEnum.SlotType.Head)
			self.remove_child(reload_progress_bar)
			head.add_child(reload_progress_bar)
			reload_progress_bar.position = Vector2.ZERO
		reload_progress_bar.global_position += reload_progress_bar_offest
	pass
func unequip():
	end_reload()
	if reload_progress_bar and is_show_reload_progress_bar:
		var parent = reload_progress_bar.get_parent()
		if parent and parent != self:
			parent.remove_child(reload_progress_bar)
		self.add_child(reload_progress_bar)
		reload_progress_bar.position = Vector2.ZERO
	super.unequip()
func discard():
	super.discard()

func _init() -> void:
	weapon_component_name = "WeaponC_Capacity"
func _process(delta: float) -> void:
	if not weapon or not weapon.enable or not enable or not inited:
		return
